<?php
/**
 * @ClassName: Payment
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-11 17:42
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\logic\PaymentLogic;
use think\Controller;
use think\facade\Log;
use Yansongda\Pay\Pay;

class Payment extends Controller
{
    protected function initialize()
    {
        $config = config()['pay']; // 初始化未配置 配置文件在 /config/pay.php
        Pay::config($config); // 初始化支付
        Log::init(['type' => 'File', 'path' => '../runtime/log/payment/']);
    }

    public function pay_order()
    {
        $order_id = input('order_id'); // 订单id
        $order_type = input('order_type'); //订单类型 order/recharge
        $pay_type = input('pay_type'); //支付方式
        $is_h5 = input('is_h5', 0);
        $is_app = input('is_app', 0);
    }

    /**
     * 微信支付回调
     * @return void
     */
    public function wxNotify()
    {
        Log::record("微信支付成功，准备写入信息");

        try {
            $callback = Pay::wechat()->callback();
            $result = $callback->toArray();

            Log::write('通知成功');
            Log::write($result);

            if ($result['summary'] == '支付成功') {
                $this->update_pay_status($result['resource']['ciphertext']['out_trade_no'], $result['resource']['ciphertext']); // 修改订单支付状态
            }
            Log::write($result);
        } catch (\Exception $e) {
            trace($e->__toString());
        }
    }

    /**
     * 支付宝支付回调
     * @return void
     */
    public function aliNotify()
    {
        Log::record("支付宝支付成功，准备写入信息");

        //支付宝支付成功通知
        try {
            $callback = Pay::alipay()->callback();
            $result = $callback->toArray();

            Log::write('通知成功');
            Log::write($result);

            if ($result['trade_status'] == 'TRADE_SUCCESS') {
                $result['transaction_id'] = $result['trade_no'];
                $this->update_pay_status($result['out_trade_no'], $result); // 修改订单支付状态
                echo 'success';
            }
        } catch (\Exception $e) {
            trace($e->__toString());
            echo "fail"; //验证失败
        }
    }

    /**
     * 处理订单支付成功通用方法
     * @param $out_trade_no
     * @param $data
     * @return void
     */
    protected function update_pay_status($out_trade_no, $data = [])
    {
        $lock = new RedisLockLogic;
        if (!$lock->lock('pay_order_' . $out_trade_no)) {
            Log::write('重复通知:' . $out_trade_no);
            return;
        }

        // out_trade_no  例如 payorder_100_1567497350; 订单类型：payorder,id:100
        $arr = explode('_', $out_trade_no);

        Log::write(json_encode($data));
        Log::write($out_trade_no);
        Log::write(json_encode($arr));

        if (count($arr) >= 2) {
            $type = $arr[0];
            $id = intval($arr[1]);
            if (isset($data['transaction_id'])) { //微信支付
                $data['pay_code'] = 'wxpay';
            } else if (isset($data['trade_no'])) { //支付宝 该交易在支付宝系统中的交易流水号。最长64位。
                $data['pay_code'] = 'alipay';
                $data['transaction_id'] = $data['trade_no'];
            }
            Log::write($id);
            if ($type == 'payorder' && $id) {
                $logic = new PaymentLogic();
                $logic->orderPaySuccess($id, $data);
            } else if ($type == 'payrecharge' && $id) {
                $logic = new PaymentLogic();
                $logic->rechargePaySuccess($id, $data);
            }
        }

        $lock->unlockAll();
    }

}